package com.querycenter.mapper;

import com.querycenter.model.DataSourceObject;
import com.querycenter.model.QueryModel;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class TestDao {
	// wyl
	public List<QueryModel> getList(Integer[] a, String tableName,
			DataSourceObject o) {
		String driver = "com.oscar.Driver";

		// String url = "jdbc:oscar://localhost:2003/osrdb";
		String jdbchead = "oscar";
		if (o.getDatatype() == 1) {
			jdbchead = "oscar";
		}

		String url = "jdbc:" + jdbchead + "://" + o.getIp() + ":" + o.getPort()
				+ "/" + o.getInstance();

		String username = o.getDatausername();
		String password = o.getDatapassword();

		Connection con = null;
		List list = new ArrayList();
		try {
			Class.forName(driver);
			con = DriverManager.getConnection(url, username, password);
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery("select * from " + tableName);

			while (rs.next()) {
				QueryModel qm = new QueryModel();
				for (int i = 0; i < a.length; i++) {
					Object value = rs.getObject(a[i].intValue());
					qm.getQueryList().add(value);
				}
				list.add(qm);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public List<QueryModel> getListByPage(Integer[] a, int begin, int end,
			DataSourceObject o, String sqlend) {
		String driver = "com.oscar.Driver";

		// String url = "jdbc:oscar://localhost:2003/osrdb";
		String jdbchead = "oscar";
		if (o.getDatatype() == 1) {
			jdbchead = "oscar";
		}

		String url = "jdbc:" + jdbchead + "://" + o.getIp() + ":" + o.getPort()
				+ "/" + o.getInstance();
		String username = o.getDatausername();
		String password = o.getDatapassword();

		Connection con = null;
		List list = new ArrayList();
		try {
			Class.forName(driver);
			con = DriverManager.getConnection(url, username, password);
			Statement st = con.createStatement();
			String sql = "";
			if (o.getType() == 1) {
				sql = "SELECT * FROM (SELECT A.*, ROWNUM RN FROM (SELECT * FROM "
						+ o.getField() + sqlend + ") A WHERE ROWNUM <= " + end
						+ ") WHERE RN >= " + begin;

			}
			if (o.getType() == 2) {
				sql = "SELECT * FROM (SELECT A.*, ROWNUM RN FROM ( select * from ("
						+ o.getField() + " ) " + sqlend + ") A WHERE ROWNUM <= "
						+ end + ") WHERE RN >= " + begin;
			}
			System.out.println(sql);
			ResultSet rs = st.executeQuery(sql);
			while (rs.next()) {
				QueryModel qm = new QueryModel();
				for (int i = 0; i < a.length; i++) {
					Object value = rs.getObject(a[i].intValue());
					qm.getQueryList().add(value);
				}
				list.add(qm);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public int getCount(DataSourceObject o, String sqlend) throws Exception {
		int counts = 0;
		String driver = "com.oscar.Driver";
		String jdbchead = "oscar";
		if (o.getDatatype() == 1) {
			jdbchead = "oscar";
		}
		String url = "jdbc:" + jdbchead + "://" + o.getIp() + ":" + o.getPort()
				+ "/" + o.getInstance();
		String username = o.getDatausername();
		String password = o.getDatapassword();
		Connection con = null;

		Class.forName(driver);

		con = DriverManager.getConnection(url, username, password);
		Statement st = con.createStatement();
		String sql = "";
		if (o.getType() == 1) {
			sql = "SELECT count(*) FROM " + o.getField() + sqlend;

		}
		if (o.getType() == 2) {
			sql = "select count(*) from (" + o.getField() + " ) " + sqlend;
		}
		System.out.println(sql);
		ResultSet rs = st.executeQuery(sql);
		if (rs.next()) {
			counts = rs.getInt(1);
		}
		return counts;
	}
}
